package boardGet;

import java.io.*;
import java.net.*;
import java.util.*;

public class getMabinogi {
	
	private final static String MABINOGI_URL = "http://www.mabinogi.jp/6th/community/";
	private final static String TRADE_LIST_URL = "tradeBoardList.asp";
	private final static String TRADE_CONTENT_URL = "tradeBoardContent.asp";
	private final static String PLAYER_LIST_URL = "playerBoardList.asp";
	private final static String PLAYER_CONTENT_URL = "playerBoardContent.asp";
	private final static String FREE_LIST_URL = "freeBoardList.asp";
	private final static String FREE_CONTENT_URL = "freeBoardContent.asp";
	private final static String MARI = "ma";
	private final static String RUEL = "ru";
	private final static String TARU = "ta";
	private final static String MORI = "mo";
	private final static String KIHO = "ki";
	private final static String TORI = "to";
	
	/**掲示板の記事取得用
	 * マビノギの取引掲示板及びプレイヤー掲示板の記事一覧を取得する
	 * @param server　取得したいサーバー
	 * @param prace　取得したい掲示板、自由掲示板には未対応
	 * @param page　取得したいページ数
	 * @return記事ナンバーをkeyにしたStringの配列の記事ナンバー、件名、名前、日付、閲覧数の順
	 * @throws MalformedURLException
	 * @throws ProtocolException
	 * @throws IOException
	 */
	public static HashMap<Integer,String[]> getMabinogiIndex(String server,String prace,int page)
		throws MalformedURLException,ProtocolException,IOException{
		
		HashMap<Integer, String[]> hm = new HashMap<Integer, String[]>();
		int number = 0;
		
		
		for(int k=1;k<=page;k++){
			URL url = new URL(MABINOGI_URL.concat(prace).concat("?sv=").concat(server).concat("&p=")+k);
			HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
			urlcon.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(urlcon.getInputStream(),"UTF-8"));
	
			while(true){
				String line = reader.readLine();
				if(line==null){
					break;
				}else if(line.contains("<!-- ▼掲示板(本文)▼-->")){
					break;
				}
			}
			for(int i=0;i<10;i++)reader.readLine();
			while(true){
				String line = reader.readLine();
				if(line==null){
					break;
				}else if(line.contains("<!-- ▲掲示板(本文)▲-->")){
					break;
				}else if(line.contains("<tr>")){
					String[] data = new String[5];//記事ナンバー、件名、名前、日付、閲覧数
					while(true){
						line=reader.readLine();
						if(line.contains("</tr>"))break;
						else if(line.replace("\t", "").equals(""))continue;
						else if(line.contains(MABINOGI_URL)){
							data[0] = line.substring(line.indexOf("&ix=")+4, line.indexOf("&ai=", line.indexOf("&ix=")));
							line = reader.readLine();
							data[1] = line.replace("\t","");
						}else if(line.contains("<td class=\"list-name\">")){
							for(int i=0;i<2;i++)reader.readLine();
							line = reader.readLine();
							data[2] = line.replace("\t","");
						}else if(line.contains("<td class=\"list-date\">")){
							line = reader.readLine();
							data[3] = line.replace("\t","");
						}else if(line.contains("<td class=\"list-view\">")){
							line = reader.readLine();
							data[4] = line.replace("\t","");						
						}
					}
					hm.put(number, data);
					number++;
				}
			}
			
			urlcon.disconnect();
		}
		return hm;
	}
	
	public static void getMabinogiIndexTest()
		throws MalformedURLException, ProtocolException, IOException{
		HashMap<Integer, String[]> hm = getMabinogiIndex(TARU,TRADE_LIST_URL,5);
		Set<Integer> keyset = hm.keySet();
		Integer[] keyint = keyset.toArray(new Integer[0]);
		Arrays.sort(keyint);
		for(int i=0;i<keyint.length;i++){
			Object[] value = hm.get(keyint[i]);
			System.out.print(keyint[i]+":");
			for(int j=0;j<value.length;j++)System.out.print(value[j]+" : ");
			System.out.println();
		}
		
	}
	
	public static void getItem(String server,String prace,int number)
		throws MalformedURLException, ProtocolException, IOException{
			
		HashMap<Integer, String[]> hm = new HashMap<Integer, String[]>();
		
			URL url = new URL(MABINOGI_URL.concat(prace).concat("?sv=").concat(server).concat("&ix=")+number);
			HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
			urlcon.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(urlcon.getInputStream(),"UTF-8"));
	
			while(true){
				String line = reader.readLine();
				if(line==null){
					break;
				}else if(line.contains("detail-title-txt")){
					System.out.println(line.replaceAll("\t", ""));
				}else if(line.contains("detail-main-contributor")){
					for(int i=0;i<4;i++)line=reader.readLine();
					System.out.println(line.replaceAll("\t", ""));
					line=reader.readLine();
					System.out.println(line.replaceAll("\t", ""));
					for(int i=0;i<6;i++)line=reader.readLine();
					System.out.println(line.replaceAll("\t", ""));
				}
			}
			urlcon.disconnect();
	}
	

	public static void main(String args[])
		throws MalformedURLException, ProtocolException, IOException{
		getItem(TARU,TRADE_CONTENT_URL,1080845);
	}
	
}
